--
-- Author: shunguo.chen
-- Date: 2017-12-26 18:46:25
--
-- PvpView pvp锦标赛

local TabsBtn = require("app.widget.TabsBtn")

-- local PvpTiantiPanel = import(".PvpTiantiPanel")
local EliteMatchPanel = import(".eliteMatch.panels.EliteMatchPanel")
local PvpChampionPanel = import(".PvpChampionPanel")
local PvpDuelPanel = import(".PvpDuelPanel")
-- local PvpChallengePanel = import(".PvpChallengePanel")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.PvpScene

function ClassRef:init()
	self._ui.Button_close:addClickEventListener(function()
			executeFunc(self._onCloseView)
		end)

	self:initTabs()

	self._panelPosX = self._ui.Node:getPositionX()
	self._panelPosY = self._ui.Node:getPositionY()
	self._ui.Node:removeFromParent()
end

-- TabsBtn
function ClassRef:initTabs()
	local tabsConfs = {}
	-- 检查精英赛是否开启
	if UIHandler:isFunctionOpen(Constant.FUNC_TYPE_CS_ELITEMATCH) then
		table.insert(tabsConfs, {index=1, name=L("CrossServerEliteTab")})
	end

	-- 检查冠军赛是否开启 or 是否通过GM命令打开了冠军赛
	if UIHandler:isFunctionOpen(Constant.FUNC_TYPE_CHAMPION) or 
		app:sendMsg("PvpController", "isChampionForceOpen") then
		table.insert(tabsConfs, {index=2, name=L("ui_csd_47")})
	end

	-- 检查决斗赛是否开启
	if UIHandler:isFunctionOpen(Constant.FUNC_TYPE_DUEL) then
		table.insert(tabsConfs, {index=3, name=L("ui_csd_931")})
	end

	local btns = {}
	for i=1,4 do
		local btn = self._ui["Panel/Panel_tab/selected_btn_" .. i]
		local conf = tabsConfs[i]
		btn:setVisible(conf ~= nil)
		if conf then
			local name1 = btn:findChild("name_1")
			if name1 then
				name1:setString(conf.name)
			end

			local name2 = btn:findChild("name_2")
			if name2 then
				name2:setString(conf.name)
			end

			btns[conf.index] = btn
		end
	end

	self._binds = {}
	local function onTabsBtn(index, btn)
		if self._curIdx then
			self._curIdx = index
			self:_switchPanel()
		else
			self._curIdx = index -- 创建tabsBtn的会默认调用一次
		end
	end
	self._tabsBtn = TabsBtn.new(btns, onTabsBtn, nil, self._binds)
end

-- 更新冠军赛页签红点
function ClassRef:refreshChampionPageRedPoint(seasonInfo)
	local btn = self._tabsBtn:getBtn(2)
	if not btn then return end
	if not btn:isVisible() then return end

	local dot = btn:getChildByName("dot")
	if seasonInfo and (seasonInfo.awardPartCount > 0 or seasonInfo.awardWonCount > 0) then
		dot:setVisible(true)
	else
		dot:setVisible(false)
	end
end

function ClassRef:show(index)
	self:setVisible(true)
	-- self:openAction(true)
	self:_switchPanel(index)
	AM.playSoundById(7012)
end

function ClassRef:checkGuide()
	if self._curIdx == 1 then
		-- 锦标赛界面
		local funcId = Constant.FUNC_TYPE_PVP
	    local params = {
	        scene = funcId,
	        sceneSelf = self,
	        getFunc = self.getGuideNodeFunc,
	        nextFunc = self.checkGuide
	    }
	    EM:notify("checkGuide", params)
	end
end
-- 获取引导node
function ClassRef:getGuideNodeFunc(id)
    if id == 451 then
        -- 切换到决斗赛
    	return self._tabsBtn:getBtn(3)
    end
end

function ClassRef:checkLabberGuide()
	-- self._tiantiPanel:checkGuide()
end

function ClassRef:getPanel(index)
	return self:createPanel(index)
end

function ClassRef:onSwitchPanel(cb)
	self._onSwitchPanel = cb
end

function ClassRef:_switchPanel(index)
	if index then
		self._curIdx = index
		self._tabsBtn:setSelected(index)
	end

	self:createPanel(self._curIdx)

	executeFunc(self._onSwitchPanel, self._curIdx)
	
	if self._curIdx == 1 then
		self:refreshPvpBoxInfo()
	end

	self:checkGuide()
end

-- 1	精英赛
-- 2	冠军赛
-- 3	决斗赛
function ClassRef:createPanel(index)
	local panel = self._binds[index]
	if not panel then
		if index == 1 then
			panel = EliteMatchPanel.create()
			self._elitePanel = panel
		elseif index == 2 then
			panel = PvpChampionPanel.create()
			self._championPanel = panel
		elseif index == 3 then
			panel = PvpDuelPanel.create()
			self._duelPanel = panel
		end
		if panel then
			panel:setPosition(self._panelPosX, self._panelPosY)
			self._ui.Panel:addChild(panel)
			if panel.onOperate then
				panel:onOperate(self._onOperate)
			end
			self._binds[index] = panel
		end
	end
	return panel
end

-- 精英赛是否显示自动tips
function ClassRef:isShowTips(b)
	if self._elitePanel then
		self._elitePanel:isShowTips(b)
	end
end

-- 刷新宝箱状态
function ClassRef:refreshPvpBoxInfo()
	if self._elitePanel then
		self._elitePanel:refreshPvpBoxInfo()
	end
end

-- 天梯赛接口
function ClassRef:updateLabberView(pvpInfo, openLvDict)
	-- self._tiantiPanel:updateView(pvpInfo, openLvDict)
end
function ClassRef:refreshLadder(data, withAction)
	-- if self._curIdx ~= 1 then return end
	-- self._tiantiPanel:refreshLadder(data, withAction)
end

-- 冠军赛接口
function ClassRef:refreshChampion(seasonInfo)
	self._championPanel:updateView(seasonInfo)
end

-- 挑战赛接口
function ClassRef:updateChallengeView(challengeInfo)
	-- self._challengePanel:updateView(challengeInfo)
end
function ClassRef:refreshTeamMemberList(team, isNewApplyerFlag)
	-- self._challengePanel:refreshTeamMemberList(team, isNewApplyerFlag)
end
function ClassRef:refreshNewApplyerFlag(visible)
    -- self._challengePanel:refreshNewApplyerFlag(visible)
end

-- 决斗赛
function ClassRef:updateDuelView(duelInfo)
	self._duelPanel:setData(duelInfo)
end

function ClassRef:onOperate(cb)
	self._onOperate = cb
end

-- 关掉界面时，移除相关定时器
function ClassRef:onExit()
    -- self._challengePanel:onExit()

    self.super.onExit(self)
end

return ClassRef
